package com.rickpan.repository;

import com.rickpan.entity.UserPrivacySettings;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.Optional;

/**
 * 用户隐私设置数据访问层
 */
@Repository
public interface UserPrivacySettingsRepository extends JpaRepository<UserPrivacySettings, Long> {

    /**
     * 根据用户ID查找隐私设置
     */
    Optional<UserPrivacySettings> findByUserId(Long userId);

    /**
     * 检查用户是否存在隐私设置
     */
    boolean existsByUserId(Long userId);

    /**
     * 删除用户的隐私设置
     */
    void deleteByUserId(Long userId);

    /**
     * 查找允许通过用户名搜索的用户
     */
    @Query("SELECT ups.userId FROM UserPrivacySettings ups WHERE ups.allowSearchByUsername = true")
    java.util.List<Long> findUsersAllowingUsernameSearch();

    /**
     * 查找允许通过邮箱搜索的用户
     */
    @Query("SELECT ups.userId FROM UserPrivacySettings ups WHERE ups.allowSearchByEmail = true")
    java.util.List<Long> findUsersAllowingEmailSearch();

    /**
     * 查找允许通过真实姓名搜索的用户
     */
    @Query("SELECT ups.userId FROM UserPrivacySettings ups WHERE ups.allowSearchByRealName = true")
    java.util.List<Long> findUsersAllowingRealNameSearch();

    /**
     * 查找允许接收好友申请的用户
     */
    @Query("SELECT ups.userId FROM UserPrivacySettings ups WHERE ups.allowFriendRequests = true")
    java.util.List<Long> findUsersAllowingFriendRequests();

    /**
     * 查找显示在线状态的用户
     */
    @Query("SELECT ups.userId FROM UserPrivacySettings ups WHERE ups.showOnlineStatus = true")
    java.util.List<Long> findUsersShowingOnlineStatus();

    /**
     * 查找自动接受团队成员好友申请的用户
     */
    @Query("SELECT ups.userId FROM UserPrivacySettings ups WHERE ups.autoAcceptTeamMembers = true")
    java.util.List<Long> findUsersAutoAcceptingTeamMembers();

    /**
     * 检查用户是否允许特定类型的搜索
     */
    @Query("SELECT CASE " +
           "WHEN :searchType = 'USERNAME' THEN ups.allowSearchByUsername " +
           "WHEN :searchType = 'EMAIL' THEN ups.allowSearchByEmail " +
           "WHEN :searchType = 'REAL_NAME' THEN ups.allowSearchByRealName " +
           "ELSE false END " +
           "FROM UserPrivacySettings ups WHERE ups.userId = :userId")
    Boolean isSearchAllowed(@Param("userId") Long userId, @Param("searchType") String searchType);

    /**
     * 检查用户是否允许接收好友申请
     */
    @Query("SELECT ups.allowFriendRequests FROM UserPrivacySettings ups WHERE ups.userId = :userId")
    Boolean isAllowingFriendRequests(@Param("userId") Long userId);

    /**
     * 检查用户是否显示在线状态
     */
    @Query("SELECT ups.showOnlineStatus FROM UserPrivacySettings ups WHERE ups.userId = :userId")
    Boolean isShowingOnlineStatus(@Param("userId") Long userId);

    /**
     * 检查用户是否自动接受团队成员好友申请
     */
    @Query("SELECT ups.autoAcceptTeamMembers FROM UserPrivacySettings ups WHERE ups.userId = :userId")
    Boolean isAutoAcceptingTeamMembers(@Param("userId") Long userId);

    /**
     * 获取隐私设置统计
     */
    @Query("SELECT " +
           "SUM(CASE WHEN ups.allowSearchByUsername = true THEN 1 ELSE 0 END) as usernameSearchCount, " +
           "SUM(CASE WHEN ups.allowSearchByEmail = true THEN 1 ELSE 0 END) as emailSearchCount, " +
           "SUM(CASE WHEN ups.allowSearchByRealName = true THEN 1 ELSE 0 END) as realNameSearchCount, " +
           "SUM(CASE WHEN ups.allowFriendRequests = true THEN 1 ELSE 0 END) as friendRequestsCount, " +
           "SUM(CASE WHEN ups.showOnlineStatus = true THEN 1 ELSE 0 END) as onlineStatusCount, " +
           "SUM(CASE WHEN ups.autoAcceptTeamMembers = true THEN 1 ELSE 0 END) as autoAcceptCount " +
           "FROM UserPrivacySettings ups")
    Object[] getPrivacySettingsStats();
}
